package data;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class POSCategory extends AbstractData {
	private long id = -1;
	private long hotelId;
	private String title;
	private long posId;
	private String description;
	private List<POSCategory> subCategory;
	private String subCategoryString;
	private String product;
	private long parentCategory;
	private boolean status;
	private boolean avaliable;
	
	public void setId(long id) {
		this.id = id;
	}
	
	@Override
	public long getId() {
		return id;
	}
	
	public long getHotelId() {
		return hotelId;
	}

	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public long getPOSId() {
		return posId;
	}

	public void setPOSId(long posId) {
		this.posId = posId;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<POSCategory> getSubCategory() {
		if ((subCategory == null || subCategory.size() == 0) && (subCategoryString != null)) {
			String[] idOfList = SplitList(subCategoryString);
			List<POSCategory> list = new ArrayList<POSCategory>();
			for (String id : idOfList) {
				POSCategory a = new POSCategory(Long.parseLong(id));
				list.add(a);
			}
			for(POSCategory a : list)
				a.LoadData();
			subCategory = list;
		}
		return subCategory;
	}

	public void setSubCategory(List<POSCategory> subCategory) {
		this.subCategoryString =CombineList(subCategory);
		this.subCategory = subCategory;
	}

	public String getSubCategoryString() {
		return subCategoryString;
	}
	public String getProduct() {
		return product;
	}

	public void setProduct(String product) {
		this.product = product;
	}
	public void setSubCategoryString(String subCategoryString) {
		this.subCategoryString = subCategoryString;
	}

	public long getParentCategory() {
		return parentCategory;
	}

	public void setParentCategory(long parentCategory) {
		this.parentCategory = parentCategory;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public boolean isAvaliable() {
		return avaliable;
	}

	public void setAvaliable(boolean avaliable) {
		this.avaliable = avaliable;
	}

	public POSCategory()
	{
		status = true;
		avaliable = true;
		parentCategory = 0;
	}
	
	public POSCategory(long id){
		this.id = id;
		this.status = true;
		this.avaliable = true;
		this.parentCategory = 0;
	}
	
	@Override
	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.POSCategory);
		else
			item = new Entity(this.key);
		item.setProperty("hotelId", this.hotelId);
		item.setProperty("title", this.title);
		item.setProperty("status", this.status);
		item.setProperty("avaliable", this.avaliable);
		item.setProperty("posId", this.posId);
		item.setProperty("description", this.description);
		item.setProperty("subCategory", this.subCategoryString);
		item.setProperty("parentCategory", this.parentCategory);
		
		return item;
	}

	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.hotelId = (Long) entity.getProperty("hotelId");
		this.title = (String) entity.getProperty("title");
		this.status = (Boolean) entity.getProperty("status");
		this.avaliable = (Boolean) entity.getProperty("avaliable");
		this.posId = (Long) entity.getProperty("posId");
		this.description = (String) entity.getProperty("description");
		this.subCategoryString = (String) entity.getProperty("subCategory");
		this.parentCategory = (Long) entity.getProperty("parentCategory");

		this.key = entity.getKey();
	}
	
	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.POSCategory, this.id);
			setEntity(entity);
		}
	}
}
